<?php

namespace app\yy\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use app\yy\model\IpRecord;
use app\yy\model\IpRequestRecord;
use app\yy\model\IpStatistics as IpStatModel;
use app\yy\model\Website as WebModel;
use app\yy\model\Domain as DoModel;

class Ip extends Admin
{
    public function getWhereMap()
    {
        $map = $this->getMap();
        $where = [];
        if ($map) {
            foreach($map as $v){
                if ($v[0] == 'domain') {
                    $domain_id = DoModel::where([$v])->column('id');
                    if ($domain_id) {
                        $where[] = [
                            'domain_id',
                            'in',
                            $domain_id
                        ];
                    } else {
                        $where[] = [
                            'domain_id',
                            '<',
                            0
                        ];
                    }
                }else{
                    $where[] = $v;
                }
            }
        }
        return $where;
    }
    public function index()
    {
        // 获取查询条件
        $map = $this->getWhereMap();
        $date_list = IpRecord::where($map)->order('date_added DESC')->paginate();
        $domain_ids = array_column($date_list->toArray()['data'],'domain_id');
        $domains = DoModel::where('id','in',$domain_ids)->column('domain','id');
        foreach ($date_list as &$value){
            $value['domain'] = $domains[$value['domain_id']] ?? '';
        }

        return ZBuilder::make('table')
            ->setPageTitle('IP列表') // 设置页面标题
            ->setTableName('ip_record') // 设置数据表名
            ->hideCheckbox()
            ->setSearchArea([
                ['text', 'domain', '网站', 'like'],
                ['text', 'http_user_agent', '浏览器', 'like'],
                ['text', 'ip_address', 'IP', 'like'],
                ['select', 'is_mobile', '设备类型', '', '', ['1'=>'移动端','0'=>'PC端']],
                ['daterange', 'date_added', '时间', '', '',['format' => 'YYYY-MM-DD HH:mm']],
            ])
            ->addColumns([ // 批量添加列
                ['id', 'ID'],
                ['domain', '域名'],
                ['ip_address', 'IP'],
                ['http_user_agent', '浏览器'],
                ['is_mobile', '设备', 'callback', function ($value) {
                    if ($value){
                        return '移动端';
                    }else{
                        return 'PC端';
                    }
                }],
                ['http_referer', '来源'],
                ['date_added', '时间'],
//                ['right_button', '操作', 'btn']
            ])
            ->setRowList($date_list) // 设置表格数据
            ->setColumnWidth([
                'id' => 40,
                'type' => 40,
                'ip' => 50,
                'pv' => 50,
                'mobile_ip_num' => 50,
                'pc_ip_num' => 50,
                'mobile_pv_num' => 50,
                'pc_pv_num' => 50,
                'right_button' => 60
            ])
            ->fetch(); // 渲染页面
    }

}